package api;

import model.Blog;
import model.BlogDao;
import model.DelDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/del")
public class DelBlog extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 尝试获取一下 queryString 中的 blogId 字段.
//        BlogDao blogDao = new BlogDao();
        String blogId = req.getParameter("blogId");
//        String blogId = req.getParameter("blogId");
        DelDao delDao = new DelDao();
        int id = Integer.parseInt(blogId);
        delDao.delete(id);
        resp.setStatus(200);


//        HttpSession session = req.getSession(false);
//        User user = (User) session.getAttribute("user");
//        String username = user.getUsername();
//        if (blogId == null) {
//            // queryString 不存在, 说明这次请求是获取博客列表
//            List<Blog> blogs = blogDao.selectAll();
//            // 需要把 blogs 转成符合要求的 json 格式字符串.
////            String respJson = objectMapper.writeValueAsString(blogs);
//            resp.setContentType("application/json; charset=utf8");
////            resp.getWriter().write(respJson);
//        } else {
//            // queryString 存在, 说明本次请求获取的是指定 id 的博客.
//            Blog blog = blogDao.selectById(Integer.parseInt(blogId));
//            if (blog == null) {
//                System.out.println("当前 blogId = " + blogId + " 对应的博客不存在!");
//            }
////            String respJson = objectMapper.writeValueAsString(blog);
//            resp.setContentType("application/json; charset=utf8");
////            resp.getWriter().write(respJson);
//        }
    }
}
